﻿using System;
using System.Collections.Generic;
using System.Text;
using BCCommon;
using BCData.IM.LeaveMessageRecord;
using BCDto.Sim.Admin.Admin;
using BCDto.IM.LeaveMessageRecord;
using BCEntity.IM.LeaveMessageRecord;
using BCService.Utils;

namespace BCService.IM.LeaveMessageRecordService
{
    /// <summary>
    /// 留言记录服务
    /// </summary>
    public class LeaveMessageRecordService : ILeaveMessageRecordService
    {
        private readonly ILeaveMessageRecordData leaveMessageRecordData;

        public LeaveMessageRecordService(ILeaveMessageRecordData leaveMessageRecordData)
        {
            this.leaveMessageRecordData = leaveMessageRecordData;
        }

        public LeaveMessageRecordDto Add(LeaveMessageRecordDto leaveMessageRecordDto)
        {
            return this.leaveMessageRecordData.Add(leaveMessageRecordDto.As<LeaveMessageRecordEntity>()).As<LeaveMessageRecordDto>();
        }

        public void Delete(DateTime? start, DateTime? end)
        {
            this.leaveMessageRecordData.Delete(start, end);
        }

        public PageableList<LeaveMessageRecordDto> Query(bool? isRead, bool? isReply, string replyAdminName, DateTime? start, DateTime? end, bool? ascending, int pageIndex, int pageSize)
        {
            var list = this.leaveMessageRecordData.Query(isRead, isReply, replyAdminName, start, end, ascending, out int dataCount, pageIndex, pageSize);
            return new PageableList<LeaveMessageRecordDto>
            {
                Count = dataCount,
                Items = list.As<IEnumerable<LeaveMessageRecordDto>>()
            };
        }

        public LeaveMessageRecordDto UpdateIsRead(long leaveMessageRecordId, AdminDto current)
        {
            return this.leaveMessageRecordData.UpdateIsRead(leaveMessageRecordId, current.AdminId, current.FullName).As<LeaveMessageRecordDto>();
        }

        public LeaveMessageRecordDto UpdateReplyInfo(long leaveMessageRecordId, LeaveMessageRecordPutDto leaveMessageRecordPutDto)
        {
            LeaveMessageRecordEntity entity = this.leaveMessageRecordData.Get(leaveMessageRecordId);
            if (entity != null && (entity.IsReply || this.leaveMessageRecordData.CheckLeaveMessageAlreadyReplied(leaveMessageRecordId)))
            {
                entity.ReplyContent += "\r\n" + entity.ReplyTime.Value.ToString("yyyy-MM-dd HH:mm:ss") + " 回复人：" + entity.ReplyUserName + "\r\n";
                entity.ReplyContent += leaveMessageRecordPutDto.ReplyContent;
                entity.ReplyTime = DateTime.Now;
                entity.ReplyUserName = leaveMessageRecordPutDto.ReplyUserName;
                entity.ReplyAdminId = leaveMessageRecordPutDto.ReplyAdminId;
            }
            else
            {
                entity = leaveMessageRecordPutDto.As<LeaveMessageRecordEntity>();
            }
            entity.LeaveMessageRecordId = leaveMessageRecordId;
            return this.leaveMessageRecordData.UpdateReplyInfo(entity).As<LeaveMessageRecordDto>();
        }

        public LeaveMessageCountDto QueryCount()
        {
            LeaveMessageCountEntity entity = this.leaveMessageRecordData.QueryCount();
            return entity.As<LeaveMessageCountDto>();
        }
    }
}
